﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using ProductionManage.Models.DataModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using test.demo.Model.WM仓储管理;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;
using wangyaxin.Jiangzhenying.ProductSalse;
using wangyaxin.Jiangzhenying.ProductSalse.Dto;
using wangyaxin.Jiangzhenying.ReturnOrders;
using wangyaxin.Jiangzhenying.ReturnOrders.Dto;
using wangyaxin.Jiangzhenying.UnitInfos.Dto;
using wangyaxin.Wang.Dto;

namespace wangyaxin.Jiangzhenying
{
    [ApiExplorerSettings(GroupName = "江珍莹")]
    public class ProductSalseService : ApplicationService, IProductSalseIService
    {

        private readonly IRepository<WM_Product_Salse, int> _repository;
        public ProductSalseService(IRepository<WM_Product_Salse, int> repository)
        {
            _repository = repository;
        }

        [HttpGet, Route("/Jiang/ShowProductSalse")]
        public async Task<string> ShowProductSalse(string? salseCode, string? soCode, string? clientName)
        {
            var prodectSalse = await _repository.GetListAsync();
            if(!string.IsNullOrEmpty(salseCode))
            {
                prodectSalse = prodectSalse.Where(x => x.SalseCode.Contains(salseCode)).ToList();
            }
            if (!string.IsNullOrEmpty(soCode))
            {
                prodectSalse = prodectSalse.Where(x => x.SoCode.Contains(soCode)).ToList();
            }
            if (!string.IsNullOrEmpty(clientName))
            {
                prodectSalse = prodectSalse.Where(x => x.ClientName.Contains(clientName)).ToList();
            }
            var UnitInfoList = ObjectMapper.Map<List<WM_Product_Salse>, List<ProductSalseDto>>(prodectSalse);
            return JsonConvert.SerializeObject(new PageDto<ProductSalseDto> { code = "", data = UnitInfoList, msg = "", count = UnitInfoList.Count });
        }

        [HttpPost, Route("/Jiang/AddProductSalse")]
        public async Task<WM_Product_Salse> AddProductSalse(WM_Product_Salse input)
        {
            var productSalse = ObjectMapper.Map<WM_Product_Salse, WM_Product_Salse>(input);
            await _repository.InsertAsync(input);
            return productSalse;
        }

        [HttpGet, Route("/Jiang/DeletProductSalse")]
        public async Task<string> DeletProductSalse(int id)
        {
            var productSalse = await _repository.GetAsync(x => x.Id == id);
            await _repository.DeleteAsync(productSalse, true);
            return JsonConvert.SerializeObject(new PageDto<ProductSalseDto> { code = "200", data = null, msg = "成功", count = 1 });
        }

        [HttpGet, Route("/Jiang/GetProductSalse")]
        public async Task<WM_Product_Salse> GetProductSalse(int id)
        {
            var productSalse = await _repository.GetAsync(id);
            return ObjectMapper.Map<WM_Product_Salse, WM_Product_Salse>(productSalse);
        }

        [HttpGet, Route("/Jiang/UpdateProductSalse")]
        public async Task<int> UpdateProductSalse(UpdateProductSalseDto input)
        {
            var wang = await _repository.GetAsync(x => x.Id == input.Id);
            wang.SalseCode = input.SalseCode;
            wang.SalseName = input.SalseName;
            wang.SoCode = input.SoCode;
            wang.ClientCode = input.ClientCode;
            wang.ClientName = input.ClientName;
            wang.SalseDate = input.SalseDate;
            wang.Status = input.Status;
            wang.Remark = input.Remark;
            wang.UpdateBy = "孙宝硕";
            wang.UpdateTime = DateTime.Now;
            await _repository.UpdateAsync(wang);
            return 1;
        }
    }
}
